package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.base.Upload;
import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.MaterialExcelService;
import com.ptswitch.material.service.MaterialService;

public class ExportMaterial extends MaterialBasePage {
    @Inject
    private MaterialService materialSer;
    @Inject
    private MaterialExcelService excelSer;

    @OnEvent(value = EventConstants.ACTIVATE)
    public Object exportMaterial() {
        List<Material> materials = materialSer.findAll();
        Upload upload = new Upload();
        upload.setFilePath("output/materials.xls");
        upload.setFileName("materials.xls");
        upload.setFileType("application/vnd.ms-excel");
        excelSer.outputMaterials(getWebUtil().getRealFilePath(upload), materials);
        return getWebUtil().download(upload);
    }
}
